package com.svn;


import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by User on 2017/3/24.
 */
public class SVNAuthzUpper {


    public static void main(String[] args) throws IOException {
        String passwd_src = "D:\\svn\\conf\\passwd";
        String passwd_dist = "D:\\svn\\conf\\passwdU";
        String authz_src = "D:\\svn\\conf\\authz";
        String authz_dist = "D:\\svn\\conf\\authzU";

        List<String> usernameList = new ArrayList<String>();

        //passwd
        FileReader passwd_src_fr = new FileReader(passwd_src);
        BufferedReader passwd_src_bf = new BufferedReader(passwd_src_fr);

        File passwd_dist_f = new File(passwd_dist);
        if (passwd_dist_f.exists()) {
            passwd_dist_f.delete();
        }
        FileWriter passwd_dist_fw = new FileWriter(passwd_dist);
        BufferedWriter passwd_dist_bw = new BufferedWriter(passwd_dist_fw);


        String valueString = null;
        while ((valueString = passwd_src_bf.readLine()) != null) {

            if (valueString != null && !"".equals(valueString.trim())) {
                String username = valueString.trim().split(":")[0];
                String password = valueString.trim().substring(valueString.trim().indexOf(":") + 1);
                //System.out.println(username.toUpperCase()+":"+password);

                passwd_dist_bw.write(username.toUpperCase() + ":" + password);
                passwd_dist_bw.newLine();


                usernameList.add(username);
            }


        }
        passwd_src_bf.close();
        passwd_src_fr.close();

        passwd_dist_bw.close();
        passwd_dist_fw.close();


//authz
        FileReader authz_src_fr = new FileReader(authz_src);
        BufferedReader authz_src_bf = new BufferedReader(authz_src_fr);

        File authz_dist_f = new File(authz_dist);
        if (authz_dist_f.exists()) {
            authz_dist_f.delete();
        }
        FileWriter authz_dist_fw = new FileWriter(authz_dist);
        BufferedWriter authz_dist_bw = new BufferedWriter(authz_dist_fw);


        String section = null;
        valueString = null;
        while ((valueString = authz_src_bf.readLine()) != null) {

            if (valueString != null && !"".equals(valueString.trim())) {
                if (valueString.trim().startsWith("[")) {
                    section = valueString.trim();
                }
                if ("[groups]".equals(section)) {
                    if (!valueString.trim().startsWith("#")) {
                        if (valueString.trim().indexOf("=") > -1) {
                            String group = valueString.trim().split("=")[0];
                            String groupUsers = valueString.trim().split("=")[1];

                            authz_dist_bw.write(group + "=" + groupUsers.toUpperCase());
                            authz_dist_bw.newLine();

                            continue;
                        }
                    }
                }
                if (!"[groups]".equals(section) && !"[aliases]".equals(section)) {
                    if (!valueString.trim().startsWith("#") && !valueString.trim().startsWith("@")) {
                        if (valueString.trim().indexOf("=") > -1) {
                            String obj = valueString.trim().split("=")[0];
                            String rw = valueString.trim().split("=")[1];

                            authz_dist_bw.write(obj.toUpperCase() + "=" + rw);
                            authz_dist_bw.newLine();

                            continue;
                        }
                    }
                }

                authz_dist_bw.write(valueString);
                authz_dist_bw.newLine();
            }


        }
        authz_src_bf.close();
        authz_src_fr.close();

        authz_dist_bw.close();
        authz_dist_fw.close();
    }

}
